import {
  allSteps
} from '@/api/zzxt/party-join'
const state = {
  init: false,
  partyJoinSteps: null,
  partyJoinStepsDict: null,
  partyJoinStepsIndex: null
}
const mutations = {
}
const actions = {
  initDictionary({
    commit,
    state
  }) {
    return new Promise((res, rej) => {
      if (state.init) return res(state)
      state.init = true
      Promise.all([allSteps()]).then(datas => {
        const toHandleList = ['partyJoinSteps']
        toHandleList.map((name, index) => {
          state[name] = datas[index] && datas[index].list
          const dict = {}
          const index_dict = {}
          state[`${name}Dict`] = dict
          state[`${name}Index`] = index_dict

          if (!state[name]) return
          const first = state[name][0]
          const value_key = (first.value === undefined) ? 'id' : 'value'
          state[name].map((i, index) => {
            dict[i[value_key]] = i
            index_dict[i[value_key]] = index
          })
        })
        res(state)
      }).catch(e => {
        state.init = false // 重置加载状态
        rej(e)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
